package com.wuliu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wuliu.auto.JSONAuthentication;
import com.wuliu.entity.ExLx;
import com.wuliu.mapper.ExLxMapper;
import com.wuliu.mapper.ExUserMapper;
import com.wuliu.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author express
 * @since 2022-03-05
 */
@RestController
@RequestMapping("/ex-lx")
public class ExLxController extends JSONAuthentication {
    @Autowired
    ExLxMapper exLxMapper;

    @Autowired
    ExUserMapper exUserMapper;

    @ApiOperation("添加联系人")
    @PostMapping("/add")
    public void addlx(HttpServletRequest request, HttpServletResponse response, @RequestParam("lxName") String lxName, @RequestParam("lxPhone") String lxPhone, @RequestParam("address") String address, @RequestParam("userId") int userId) throws Exception{
        ExLx exLx=new ExLx();
        exLx.setLxName(lxName);
        exLx.setAddress(address);
        exLx.setLxPhone(lxPhone);
        exLx.setUserId(userId);
        if(exLxMapper.insert(exLx)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除联系人")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId ) throws Exception{
        int i=exLxMapper.deleteById(userId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改联系人电话")
    @PostMapping("/updPhone")
    public void updPhone(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("phone") String  phone)throws Exception{
        ExLx exLx=exLxMapper.selectById(id);
         exLx.setLxPhone(phone);
        if(exLxMapper.updateById(exLx)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改联系人地址")
    @PostMapping("/updAddress")
    public void updAddress(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id,@RequestParam("address") String  address)throws Exception{
        ExLx exLx=exLxMapper.selectById(id);
        exLx.setLxPhone(address);
        if(exLxMapper.updateById(exLx)>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户查找联系人")
    @PostMapping("/findByUserId")
    public void findByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId") int   userId,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<ExLx> shUserPage=new Page<>(current,size);
        Page<ExLx> shUserPages=exLxMapper.selectPage(shUserPage,wrapper);
        List<ExLx> records = shUserPages.getRecords();
        int total=exLxMapper.selectCount(wrapper);
        for(ExLx a:records){
            a.setExUser(exUserMapper.selectById(a.getUserId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("该用户还没有联系人");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找所有用户的所有与联系人")
    @PostMapping("/findAllUser")
    public void findAllUser(HttpServletRequest request, HttpServletResponse response,@RequestParam("size") int size,@RequestParam ("current") int current)throws  Exception{
        Page<ExLx> shUserPage=new Page<>(current,size);
        Page<ExLx> shUserPages=exLxMapper.selectPage(shUserPage,null);
        List<ExLx> records = shUserPages.getRecords();
        int total=exLxMapper.selectCount(null);
        for(ExLx a:records){
            a.setExUser(exUserMapper.selectById(a.getUserId()));
        }
        if(records.size()>0){
            Result ok = Result.ok().data("userList",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

}

